package uk.ac.cam.echo.server.integrationtests;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import uk.ac.cam.echo.server.Database;

/**
 * @author Tom Smith
 */
public class TestLocationTable extends TestDatabase {

	@Test(expected=SQLException.class)
	/**
	 * Insert duplicate location into the table.
	 */
	public void duplicateLocation() throws SQLException {
		// Execute SQL statement.
		Statement sqlStatement = super.m_Connection.createStatement();
		sqlStatement.execute("INSERT INTO " + Database.tblLocation + " VALUES('1', 'Chapel Court');");
		sqlStatement.execute("INSERT INTO " + Database.tblLocation + " VALUES('2', 'Chapel Court');");
		sqlStatement.close();

		// Commit changes to file, and release DB locks.
		super.m_Connection.commit();

		fail("Duplicate location added to the table.");
	}

	@Test
	/**
	 * Insert one location into the table.
	 */
	public void insertLocation() {
		try {
			// Execute SQL statement.
			Statement sqlStatement = super.m_Connection.createStatement();
			sqlStatement.execute("INSERT INTO " + Database.tblLocation + " VALUES('1', 'Chapel Court');");
			sqlStatement.close();

			// Commit changes to file, and release DB locks.
			super.m_Connection.commit();

		} catch (SQLException e) {
			fail("An SQL Exception occured - " + e.getMessage());
		}
	}

	@Test
	/**
	 * Insert many locations into the table.
	 */
	public void insertMulipleLocation() {
		try {
			// Execute SQL statement.
			Statement sqlStatement = super.m_Connection.createStatement();

			for (int i = 0; i < 100; i++) {
				sqlStatement.execute("INSERT INTO " + Database.tblLocation + " VALUES('" + i + "', '" + i + "');");
			}
			sqlStatement.close();

			// Commit changes to file, and release DB locks.
			super.m_Connection.commit();

		}  catch (SQLException e) {
			fail("An SQL Exception occured - " + e.getMessage());
		}
	}
}
